package com.ruoyi.GooverGUI.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.Course.service.impl.StudyCourseServiceImpl;
import com.ruoyi.Plancontent.domain.StudyPlancontent;
import com.ruoyi.Plancontent.service.IStudyPlancontentService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.plan.domain.StydyPlan;
import com.ruoyi.plan.mapper.StydyPlanMapper;
import com.ruoyi.plan.service.IStydyPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.GooverGUI.mapper.GooverGUIMapper;
import com.ruoyi.GooverGUI.domain.GooverGUI;
import com.ruoyi.GooverGUI.service.IGooverGUIService;

/**
 * GooverGUIService业务层处理
 * 
 * @author luobom
 * @date 2021-12-08
 */
@Service
public class GooverGUIServiceImpl implements IGooverGUIService 
{
    @Autowired
    private GooverGUIMapper gooverGUIMapper;
    @Autowired
    StudyCourseServiceImpl studyCourseService ;
    @Autowired
    private IStydyPlanService stydyPlanService;
    @Autowired
    private StydyPlanMapper stydyPlanMapper;

    /**
     * 查询GooverGUI
     * 
     * @param id GooverGUI主键
     * @return GooverGUI
     */
    @Override
    public GooverGUI selectGooverGUIById(Long id)
    {
        return gooverGUIMapper.selectGooverGUIById(id);
    }

    //复习结束
    @Override
    public AjaxResult submitover(){
        //获取复习计划
        StydyPlan spc=new StydyPlan();
        List<StydyPlan> Listspc=stydyPlanService.selectStydyPlanList(spc); //获取计划
        List<StydyPlan> Listspc2=new ArrayList<>(); //存储需要执行的计划
        //判断那些计划需要今天执行
        for(StydyPlan sp:Listspc){
            String[] jd=sp.getGoovereplan().split("\\.");
            String str = DateUtil.format(sp.getPlantime(),"yyyy-MM-dd");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date myDate = formatter.parse(str);
                Calendar c = Calendar.getInstance();
                c.setTime(myDate);
                c.add(Calendar.DATE, Integer.valueOf(sp.getPlanstage()));
                myDate = c.getTime();

                if(ifdatebigandsmalle(formatter.format(myDate),1)||ifdatebigandsmalle(formatter.format(myDate),3)){ //如果当前阶段就是今天则进行复习
                    //正常的计划执行 || 已经错过的计划执行
                    Listspc2.add(sp);
                }
            } catch (ParseException e1) {
                e1.printStackTrace();
            }
        }

        //修改阶段
        for(StydyPlan sp:Listspc2){
            String[] jd=sp.getGoovereplan().split("\\.");
                for(int i=0;i<jd.length;i++){
                    if(jd[i].equals(sp.getPlanstage())){
                        if(++i==jd.length){
                            sp.setExist("已完成");
                            break;
                        }else{
                            sp.setPlanstage(jd[i]);
                        }
                    }
                }
            stydyPlanMapper.updateStydyPlan(sp);
        }
        return AjaxResult.success(Listspc2);
    }


    /**
     * 查询GooverGUI列表  执行此语句可以获得需要复习的单词
     * 
     * @param gooverGUI GooverGUI
     * @return GooverGUI
     */
    @Override
    public List<GooverGUI> selectGooverGUIList(GooverGUI gooverGUI)
    {
        //获取课程
        String course=studyCourseService.GetDefaultCourse().getUid();
        //查询课程
        gooverGUI.setCourseuid(course);
        gooverGUI.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
//        List<GooverGUI> gm=gooverGUIMapper.selectGooverGUIList(gooverGUI);

        //获取复习计划
        StydyPlan spc=new StydyPlan();
        List<StydyPlan> Listspc=stydyPlanService.selectStydyPlanList(spc); //获取计划
        List<StydyPlan> Listspc2=new ArrayList<>(); //存储需要执行的计划
        //判断那些计划需要今天执行
        for(StydyPlan sp:Listspc){
            String[] jd=sp.getGoovereplan().split("\\.");
            String str = DateUtil.format(sp.getPlantime(),"yyyy-MM-dd");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date myDate = formatter.parse(str);
                Calendar c = Calendar.getInstance();
                c.setTime(myDate);
                c.add(Calendar.DATE, Integer.valueOf(sp.getPlanstage()));
                myDate = c.getTime();

                if(ifdatebigandsmalle(formatter.format(myDate),1)||ifdatebigandsmalle(formatter.format(myDate),3)){ //如果当前阶段就是今天则进行复习
                    //正常的计划执行 || 已经错过的计划执行
                    Listspc2.add(sp);
                }
            } catch (ParseException e1) {
                e1.printStackTrace();
            }

        }
        if(Listspc2.size()==0){
            List<GooverGUI> lg =new ArrayList<>();
            return lg;}
        //根据计划时间获取单词
        List<String> liststr=new ArrayList<>();

        for(StydyPlan st:Listspc2){
            liststr.add(DateUtil.format(st.getPlantime(),"yyyy-MM-dd"));
        }
        gooverGUI.setCreateList(liststr);

        return gooverGUIMapper.selectGooverGUIList(gooverGUI);
    }


    //比较时间大小
    public Boolean ifdatebigandsmalle(String a,int state){ //state 1== 2> 3<
        // 比较 年 月 日
        SimpleDateFormat sdf  =new SimpleDateFormat("yyyy-MM-dd"); //创建日期转换对象：年 月 日
        String date = a; //假设 设定日期是  2018-11-11
        Date today = new Date();
        try {
            Date dateD = sdf.parse(date); //将字符串转换为 date 类型  Debug：Sun Nov 11 00:00:00 CST 2018

            boolean flag=false;
            switch (state){
                case 1:{
                    flag=Long.valueOf(dateD.getTime()).equals(today.getTime());
                    break;
                }
                case 2:{
                    flag=dateD.getTime() > today.getTime();
                    break;
                }
                case 3:{
                    flag=dateD.getTime() < today.getTime();
                    break;
                }

            }
            return flag;
        } catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return false;
        }
    }

    /**
     * 新增GooverGUI
     * 
     * @param gooverGUI GooverGUI
     * @return 结果
     */
    @Override
    public int insertGooverGUI(GooverGUI gooverGUI)
    {
        gooverGUI.setCreateTime(DateUtils.getNowDate());
        return gooverGUIMapper.insertGooverGUI(gooverGUI);
    }

    /**
     * 修改GooverGUI
     * 
     * @param gooverGUI GooverGUI
     * @return 结果
     */
    @Override
    public int updateGooverGUI(GooverGUI gooverGUI)
    {
        gooverGUI.setUpdateTime(DateUtils.getNowDate());
        return gooverGUIMapper.updateGooverGUI(gooverGUI);
    }

    /**
     * 批量删除GooverGUI
     * 
     * @param ids 需要删除的GooverGUI主键
     * @return 结果
     */
    @Override
    public int deleteGooverGUIByIds(Long[] ids)
    {
        return gooverGUIMapper.deleteGooverGUIByIds(ids);
    }

    /**
     * 删除GooverGUI信息
     * 
     * @param id GooverGUI主键
     * @return 结果
     */
    @Override
    public int deleteGooverGUIById(Long id)
    {
        return gooverGUIMapper.deleteGooverGUIById(id);
    }
}
